package struts.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import struts.form.HelloForm;

public class HelloAction extends Action{
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("UTF-8"); 
		HelloForm helloForm=(HelloForm)form;
		String info=helloForm.getInfo();
		String sex=helloForm.getSex();
		String moString=info+sex;
		
		moString= new String(moString.getBytes("ISO-8859-1"),"UTF-8");  
		/*Tomcat中对于post方法提交表单时，默认采用的编码方式是ISO-8859-1，
		因此在我们获得了username这个字符串的时候还得将ISO-8859-1编码的字
		符串转换为UTF-8的字符串。在JavaSE中的String类有一个构造方法可以
		讲指定的byte数组转换(解码)成指定字符编码的字符串形式。*/
		
		if (info.length()>5) {
			ActionMessages mes=new ActionMessages();
			mes.add("info",new ActionMessage("msg.info"));
			super.saveMessages(request, mes);			
			return mapping.getInputForward();
		} else {
			request.setAttribute("msg", moString);
			return mapping.findForward("show");
		}
		
		
		
	}
}
